﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GlobalObjects;

namespace DataAccessLayer
{
    public class TestimonialEntity
    {
        /// <summary>
        /// insert the given Testimonial object into the database
        /// </summary>
        /// <param name="testi">testimonial data</param>
        public void insertToTestimonials_DAL(Testimonial testi)
        {
            using (var context = new MSR2DBEntities())
            {
                var nTesti = new testimonial
                {
                    fullname = testi.Fullname,
                    email = testi.Email,
                    subject = testi.Subject,
                    message = testi.Message,
                    time = testi.Time
                };
                context.testimonials.Add(nTesti);
                context.SaveChanges();
            }
        }
        /// <summary>
        /// returns a list of all the available testimonials from the database
        /// </summary>
        /// <returns>List of all the available testimonials</returns>
        public List<Testimonial> getAllTestimonials_DAL(){
            List<Testimonial> list = new List<Testimonial>();
            using (MSR2DBEntities ctx = new MSR2DBEntities())
            {
                var query = from t in ctx.testimonials
                            select t;
                foreach(var test in query){
                    Testimonial testimonial = new Testimonial();
                    testimonial.Fullname = test.fullname;
                    testimonial.Email = test.email;
                    testimonial.Subject = test.subject;
                    testimonial.Message = test.message;
                    testimonial.Time = test.time.Value;
                    list.Add(testimonial);
                }
                return list;
            }
        }

    }
}
